package drive.Service.ServiceFormat;

import com.google.gson.Gson;
import drive.Mapper.ChatSessionMapper;
import drive.Model.ChatSession;
import drive.Service.ServiceInterface.ChatSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatSessionServiceA implements ChatSessionService {
    @Autowired
    private ChatSessionMapper chatSessionMapper;

    private Gson gson = new Gson();

    @Override
    public String findAll() {
        List<ChatSession> list = chatSessionMapper.findAll();
        return gson.toJson(list);
    }

    @Override
    public String findByChatSessionID(int chatSessionID) {
        ChatSession chatSession = chatSessionMapper.findByChatSessionID(chatSessionID);
        return gson.toJson(chatSession);
    }

    @Override
    public String findByUserIDs(int user1ID, int user2ID) {
        ChatSession chatSession = chatSessionMapper.findByUserIDs(user1ID, user2ID);
        return gson.toJson(chatSession);
    }

    @Override
    public String add(ChatSession chatSession) {
        ChatSession check = chatSessionMapper.findByUserIDs(chatSession.getUser1ID(),chatSession.getUser2ID());
        if(check==null){
            int result = chatSessionMapper.add(chatSession);
            return "创建会话成功";
        }

        return "会话已存在";
    }

    @Override
    public String update(ChatSession chatSession) {
        int result = chatSessionMapper.update(chatSession);
        return gson.toJson(result);
    }

    @Override
    public String delete(int chatSessionID) {
        int result = chatSessionMapper.delete(chatSessionID);
        return "会话已删除";
    }
}
